package com.pwc.conversionTool.rest;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pwc.conversionTool.Manager.SoapUIManager;
import com.pwc.conversionTool.Manager.XMLConfigImpl;


@Controller
public class XMLConfigRest {

	private static final Logger logger = LoggerFactory
			.getLogger(XMLConfigRest.class);

	@Autowired
	public ServletContext context;

	/**
	 * get the config file data
	 * 
	 * @return
	 */
	@RequestMapping(value = "/rest/config/xmlConfigData", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
	public @ResponseBody String getXMLConfig() {

		String xmlData = "";
		try {
			XMLConfigImpl configImpl = XMLConfigImpl.initial(context);
			xmlData = configImpl.getXMLString();
		} catch (Exception e) {
			logger.error("log4j is running in xmlConfigData");
			logger.error(Arrays.toString(e.getStackTrace()));
		}
		return xmlData;
	}

	/**
	 * save data
	 * 
	 * @return
	 */
	@RequestMapping(value = "/rest/config/SaveXmlData", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
	public @ResponseBody void setXMLConfig(String xmlData) {

		try {
			XMLConfigImpl configImpl = XMLConfigImpl.initial(context);
			configImpl.saveDocument(xmlData);
		} catch (Exception e) {
			logger.error("log4j is running in SaveXmlData");
			logger.error(Arrays.toString(e.getStackTrace()));
		}

	}

	/**
	 * connect to endpoint and save to config
	 * 
	 * @param endpoint
	 * @return
	 */
	@RequestMapping(value = "/rest/config/endpoint", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
	public @ResponseBody String endPoint(String endpoint) {

		XMLConfigImpl xmlConfigImpl;

		boolean process = true;
		String message = "";
		try {
			SoapUIManager.settingEndpoint(endpoint);
		} catch (Exception e) {
			process = false;
			message = "error:" + e.getMessage();
		}
		if (process) {
			try {
				xmlConfigImpl = XMLConfigImpl.initial(context);
				xmlConfigImpl.setEndpoint(endpoint);
				xmlConfigImpl.saveDocument();
				message = "success! server connects to endpoint:"
						+ SoapUIManager.getUrl();
			} catch (DocumentException e) {
				message = "error:" + e.getMessage();
			} catch (IOException e) {
				message = "error:" + e.getMessage();
			}catch (Exception e) {
				logger.error("error in setEndpoint");
				logger.error(Arrays.toString(e.getStackTrace()));
			}
		}
		return message;
	}


	/**
	 * 
	 * @param name
	 * @param value
	 * @return
	 */
	@RequestMapping(value = "/rest/config/addFolderNode", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
	public @ResponseBody String addFolderNode(String key, String value) {	
		String resultString="";
		try {
			resultString= XMLConfigImpl.initial(context).addFolderNode(key, value)?"success to addXpath":"failed to addXpath";
		} catch (Exception e) {
			logger.error("error in addFolderNode");
			logger.error(Arrays.toString(e.getStackTrace()));
		}
		return resultString;
	}


	@RequestMapping(value = "/rest/config/getNodeByXpath", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Map<String, String> getNodeByXpath() {

		Map<String, String> hashMap = new HashMap<String, String>();		
		try {			
			hashMap = XMLConfigImpl.initial(context).getXMLMap("//config/application");
		}catch (Exception e) {
			logger.error("error in getNodeByXpath");
			logger.error(Arrays.toString(e.getStackTrace()));
		}
		return hashMap;
	}

	@RequestMapping(value = "/rest/config/deleteFolderNode", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
	public @ResponseBody void deleteFolderNode(String key) {
		try {
			XMLConfigImpl.initial(context).deleteFolderNode(key);		
		} catch (Exception e) {
			logger.error("error in deleteFolderNode");
			logger.error(Arrays.toString(e.getStackTrace()));
		}	
	}


	/**
	 * save user name and password
	 * 
	 * @return
	 */
	@RequestMapping(value = "/rest/config/UserNamePassword", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody void setUserNamePassword(String userName,
			String password, String passwordType) {

		try {
			XMLConfigImpl configImpl = XMLConfigImpl.initial(context);
			configImpl.setUserName(userName);
			configImpl.setPassword(password);
			configImpl.setPasswordType(passwordType);
			configImpl.saveDocument();
		} catch (Exception e) {
			logger.error(" error in UserNamePassword");
			logger.error(Arrays.toString(e.getStackTrace()));
		}
	}

}
